package com.thirtydays.oral.common.service;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.SqlConnRunner;
import cn.hutool.json.JSONObject;
import java.sql.Connection;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author Dell
 * @version 1.0
 * @date 2021/9/24 14:54
 */
@Service
@Profile({"one"})
public class DBServiceOneImpl implements DBService {
    private static final Logger log = LoggerFactory.getLogger(DBServiceOneImpl.class);

    public DBServiceOneImpl() {
    }

    @Override
    public void insert(Connection connection, JSONObject parseObj) {
        log.info("开始处理新增消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            Integer nowTime = Integer.parseInt(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            String realname = parseObj.getStr("realname");
            String gender = parseObj.getStr("gender");
            String patientNo = parseObj.getStr("patientNo");
            if ("MALE".equals(gender)) {
                gender = "M";
            } else {
                gender = "F";
            }
            Long birthDate = (Long)parseObj.get("birthDate");
            LocalDateTime
                localDateTime = LocalDateTime.ofEpochSecond(birthDate / 1000L, 0, ZoneOffset.ofHours(8));
            int birthTime = Integer.parseInt(localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            String patientId = parseObj.getStr("patientId");
            sqlConnRunner.insert(connection, Entity
                .create().setTableName("RRM_Person")
                .set("history_id", 0).set("status", 1).set("updated_by", 0)
                .set("updated_date", nowTime).set("updated_time", nowTime)
                .set("first_name", realname).set("last_name", realname).set("gender", gender)
                .set("date_of_birth", birthTime).set("maiden_name", patientId).set("external_id", patientNo));
            List<Entity> entities = sqlConnRunner.find(connection, Entity.create().setTableName("RRM_Person").set("maiden_name", patientId));
            Entity entity = entities.get(0);
            Object personId = entity.get("person_id");
            sqlConnRunner.insert(connection, Entity.create().setTableName("RRM_Person_Attrib").set("history_id", 0).set("person_id", personId).set("status", 1).set("updated_by", 0).set("updated_date", nowTime).set("updated_time", nowTime).set("attrib_type", 1).set("int_value", 1));
        } catch (Exception var14) {
            log.error("SQL错误信息: {}", var14.getMessage());
        }

    }

    @Override
    public void update(Connection connection, JSONObject parseObj) {
        log.info("开始处理修改消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String realname = parseObj.getStr("realname");
            String gender = parseObj.getStr("gender");
            Long birthDate = (Long)parseObj.get("birthDate");
            String patientId = parseObj.getStr("patientId");
            LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(birthDate / 1000L, 0, ZoneOffset.ofHours(8));
            int birthTime = Integer.parseInt(localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
            if ("MALE".equals(gender)) {
                gender = "M";
            } else {
                gender = "F";
            }

            sqlConnRunner.update(connection, Entity.create().setTableName("RRM_Person").set("first_name", realname).set("last_name", realname).set("gender", gender).set("date_of_birth", birthTime), Entity.create().setTableName("RRM_Person").set("maiden_name", patientId));
        } catch (Exception var10) {
            log.error("SQL错误信息: {}", var10.getMessage());
        }

    }

    @Override
    public void delete(Connection connection, JSONObject parseObj) {
        log.info("开始处理删除消息：{}", parseObj);

        try {
            SqlConnRunner sqlConnRunner = DbUtil.newSqlConnRunner(connection);
            String patientId = parseObj.getStr("patientId");
            sqlConnRunner.del(connection, Entity.create().setTableName("RRM_Person").set("maiden_name", patientId));
        } catch (Exception var5) {
            log.error("SQL错误信息: {}", var5.getMessage());
        }

    }
}
